﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Business.TranferParam;
using Newtonsoft.Json;
using System.Text;
using DotNet.Common.TransferParam;

namespace ZLCMS.Web.SystemManage.Basics.Param.Group
{
    public partial class Edit : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected SysParam _gourpDB;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                SaveUser();
            }
        }

        protected void InitializeGlobalValue()
        {
            string groupId;

            try
            {
                if (!IsPostBack)
                {
                    groupId = Request["groupid"] == null ? "" : Request["groupid"];
                    _groupId.Text = groupId;
                }
                else
                {
                    groupId = _groupId.Text;
                }

                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    _gourpDB = session.CreateCriteria<SysParam>().Add(Expression.Eq("GroupCode", "TranferParam")).Add(Expression.Eq("ItemCode", groupId)).UniqueResult<SysParam>();
                }

                if (_gourpDB == null)
                {
                    throw new Exception("找不到对应信息.");
                }
            }
            catch (System.Exception ex)
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                return;
            }
        }

        protected void InitializeContent()
        {
            //页面控件数据

            //页面控件值
            if (!IsPostBack)
            {
                tb_code.Text = _gourpDB.ItemCode;
                tb_value.Text = _gourpDB.ItemValue;
                tb_memo.Text = _gourpDB.Memo;
                tb_param1.Text = _gourpDB.Param1;
                tb_param2.Text = _gourpDB.Param2;

                if (!string.IsNullOrEmpty(_gourpDB.Param5))
                {
                    grid.DataSource = JsonConvert.DeserializeObject<List<ParamGroupExp>>(_gourpDB.Param5);
                    grid.DataBind();
                }
            }
        }

        protected void SaveUser()
        {
            _gourpDB.ItemCode = tb_code.Text;
            _gourpDB.ItemValue = tb_value.Text;
            _gourpDB.Memo = tb_memo.Text;
            _gourpDB.Param1 = tb_param1.Text;
            _gourpDB.Param2 = tb_param2.Text;

            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                session.Update(_gourpDB);
                session.Flush();
            }

            _framePage.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                callbackType = "closeCurrent",
                navTabId = _framePage.NavTabId
            });
        }
    }
}